<?php

namespace App\Http\Controllers;

use App\User;
use App\Utils\PageUtil;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    function getApiUser(Request $request)
    {
        $apiToken = $request->header('API-TOKEN');

        if (!empty($apiToken)){
            /** @var User $user */
            $user = User::where("api_token", $apiToken)->first();

            //未授权
            return $user;
        }

        return null;
    }

    /**
     * 调用成功
     * @param array $data
     * @param string $message
     * @param int $code
     * @return array
     */
    function success($data = [], $message = '', $code = 200)
    {
        return ['code' => $code, 'data' => $data, 'msg' => $message];
    }


    /**
     * 调用失败
     * @param string $msg
     * @param int $code
     * @return array
     */
    function error($msg = '参数格式错误', $code = 400)
    {
        return ['code' => $code, 'msg' => $msg];
    }


    function page($list = [], $page = 1, $pageSize = 10, $totalRecord = 0)
    {
        return [
            'code' => 200,
            'data' => PageUtil::page($list, $page, $pageSize, $totalRecord),
        ];
    }


}
